package org.jivesoftware.openfire.findpwd;

import java.beans.IntrospectionException;
import java.lang.reflect.InvocationTargetException;
import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.ResultSetMetaData;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import org.jivesoftware.database.DbConnectionManager;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

public class DbFindPwdManager {
	private DbFindPwdManager() {
    }
	public static DbFindPwdManager getInstance() {
        return findPwdManager;		
	}
	private static final Logger log = LoggerFactory.getLogger(DbFindPwdManager.class);
	private static final DbFindPwdManager findPwdManager = new DbFindPwdManager();
	private static final String QUERY_EMAIL_COUNT = "select  count(*) AS emailCount from ofuser where email = ? and username = ?";
	private static final String QUERY_FIND_PWD = "select username,encryptedPassword,email from ofuser where email = ?";
	public static Connection getConnection(){
		Connection con = null;
		 try {		
			 con = DbConnectionManager.getConnection();
			
		}
		 catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			log.debug("DriverManager.getConnection(url,user,pwd) failed");
		}
		return con;
	}
	
	public HashMap<String,Object> queryEmailCount(String email,String username){
		Connection con = getConnection();
        PreparedStatement pstmt = null;
        HashMap<String, Object> result = new HashMap<String, Object>();
        try {
            pstmt = con.prepareStatement(QUERY_EMAIL_COUNT);
            int i =1;
            pstmt.setString(i++, email);
            pstmt.setString(i++, username);

            ResultSet rs = pstmt.executeQuery();
           
            int emailCount = 0 ;
            while (rs.next()) {  
            	emailCount = rs.getInt("emailCount");

            }
            result.put("emailCount",emailCount);
            log.info("emailCount:"+emailCount);
            return result;
        } catch (SQLException sqle) {
            log.info("queryEmailCount exception: {}", sqle);
            return result;
        } finally {
        		 DbConnectionManager.closeConnection(pstmt, con);
        }
}
	public FindPwdBean queryFindPwd(String email){
		Connection con = getConnection();
		PreparedStatement pstmt = null;
		FindPwdBean result = new FindPwdBean();
		 HashMap<String, Object> map = new HashMap<String, Object>();  
		try {
			pstmt = con.prepareStatement(QUERY_FIND_PWD);
			 int i =1;
	         pstmt.setString(i, email);
	         ResultSet rs = pstmt.executeQuery();
             ResultSetMetaData rsmd = rs.getMetaData();
             int columnCount = rsmd.getColumnCount();     
             while (rs.next()) {  
                 for (int j = 1; j <= columnCount; ++j) {  
                     String columnVal = rs.getString(j);  
                     if (columnVal == null) {  
                         columnVal = "";  
                     }  
                     map.put(rsmd.getColumnName(j), columnVal);  
                 }  
                log.info(map.toString());
             }  
             result.setUserName(map.get("username").toString());
             result.setEncryptedPassword(map.get("encryptedPassword").toString());
             result.setEmail(map.get("email").toString());
             log.info(map.get("encryptedPassword").toString());
             return result;
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
			log.info(" queryFindPwd failed");
			return result;
		}finally{
			 DbConnectionManager.closeConnection(pstmt, con);
		}
		 
	}
}
